{% extends "base.html" %}

{% block title %}分类管理 - 提示词管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1>
                <i class="bi bi-folder"></i> 
                分类管理
                <span class="badge bg-primary">{{ categories|length }}</span>
            </h1>
            <a href="{{ url_for('add_category') }}" class="btn btn-primary">
                <i class="bi bi-plus-circle"></i> 新建分类
            </a>
        </div>
    </div>
</div>

<!-- 分类列表 -->
<div class="row">
    {% for category in categories %}
    <div class="col-md-6 col-lg-4 mb-4">
        <div class="card h-100 category-card fade-in">
            <div class="card-body">
                <div class="d-flex align-items-center mb-3">
                    {% if category.color %}
                        <div class="category-color" style="background-color: {{ category.color }}"></div>
                    {% else %}
                        <div class="category-color" style="background-color: #6c757d"></div>
                    {% endif %}
                    <h5 class="card-title mb-0">{{ category.name }}</h5>
                    {% if not category.enabled %}
                        <span class="badge bg-secondary ms-2">已禁用</span>
                    {% endif %}
                </div>
                
                {% if category.description %}
                <p class="card-text text-muted mb-3">{{ category.description }}</p>
                {% endif %}
                
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <span class="badge bg-info">
                            <i class="bi bi-chat-square-text"></i>
                            {{ category.prompt_count or 0 }} 个提示词
                        </span>
                    </div>
                    <div class="btn-group" role="group">
                        <a href="{{ url_for('edit_category', category_id=category.id) }}" 
                           class="btn btn-outline-primary btn-sm" 
                           title="编辑分类">
                            <i class="bi bi-pencil"></i>
                        </a>
                        {% if category.id != 'default' %}
                        <button type="button" 
                                class="btn btn-outline-danger btn-sm" 
                                title="删除分类"
                                onclick="deleteCategory('{{ category.id }}', '{{ category.name }}')">
                            <i class="bi bi-trash"></i>
                        </button>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endfor %}
    
    {% if not categories %}
    <div class="col-12">
        <div class="text-center py-5">
            <i class="bi bi-folder2-open" style="font-size: 4rem; color: #dee2e6;"></i>
            <h3 class="text-muted mt-3">暂无分类</h3>
            <p class="text-muted">点击上方按钮创建第一个分类</p>
            <a href="{{ url_for('add_category') }}" class="btn btn-primary">
                <i class="bi bi-plus-circle"></i> 新建分类
            </a>
        </div>
    </div>
    {% endif %}
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除分类 <strong id="categoryName"></strong> 吗？</p>
                <p class="text-warning"><i class="bi bi-exclamation-triangle"></i> 删除后该分类下的所有提示词将移动到默认分类。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
.category-card {
    transition: transform 0.2s, box-shadow 0.2s;
}
.category-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
</style>
{% endblock %}

{% block extra_js %}
<script>
function deleteCategory(categoryId, categoryName) {
    document.getElementById('categoryName').textContent = categoryName;
    document.getElementById('deleteForm').action = '/categories/' + categoryId + '/delete';
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}
</script>
{% endblock %}